package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import java.util.Arrays;

import static com.shanxia.codec.Decoder.*;

/**
 * 充电桩上报 BMS 信息
 * TODO demo乱码
 */
@Data
public class Cmd304 extends ProtocolDataBase {

    public static final int MIN_SIZE = 246;

    private int v1;
    private int v2;
    private String sn;                                  // 充电桩编码
    private int workingStatus;                          // 工作状态
    private int carConnStatus;                          // 车连接状态
    private String carRecoMsg;                          // BRM 车辆辨识报文
    private String vbiMsg;                              // VBI 报文
    private String bcpBattChargingParam;                // BCP动力蓄电池充电参数
    private String broBattChargingReadyStatus;          // BRO电池充电准备就绪状态
    private String bclBattChargingRequirement;          // BCL 电池充电需求
    private String bcsBattChargingTotalStatus;          // BCS 电池充电总状态
    private String bsmBattStatus;                       // BSM 动力蓄电池状态信 息
    private String bstStopCharging;                     // BST 中止充电
    private String bsdStatistics;                       // BSD BMS 统计数据
    private String bemMsg;                              // BEM 报文

    public Cmd304(){
        super.setCmd(304);
    }

    public static Cmd304 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd304 cmd = new Cmd304();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        cmd.v1 = getUint16(dataField, offset);
        offset += 2;
        cmd.v2 = getUint16(dataField, offset);
        offset += 2;
        //32字节
        cmd.sn = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 32), true);
        offset += 32;
        // 1字节工作状态
        cmd.workingStatus = getUint8(dataField[offset++]);
        // 1字节车连接状态
        cmd.carConnStatus = getUint8(dataField[offset++]);
        // 64字节车辆识别报文
        cmd.carRecoMsg = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 32), false);
        offset += 64;
        // 64字节VBI报文
        cmd.vbiMsg = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 32), false);
        offset += 64;
        // 16字节BCP 动力蓄电池充电参 数
        cmd.bcpBattChargingParam = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 16), false);
        offset += 16;
        // 8字节BRO 电池充电准备就绪 状态
        cmd.broBattChargingReadyStatus = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 8), false);
        offset += 8;
        // 8字节BCL 电池充电需求
        cmd.bclBattChargingRequirement = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 8), false);
        offset += 8;
        // 16字节BCS 电池充电总状态
        cmd.bcsBattChargingTotalStatus = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 16), false);
        offset += 16;
        // 8字节BSM 动力蓄电池状态信 息
        cmd.bsmBattStatus = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 8), false);
        offset += 8;
        // 8字节BST 中止充电
        cmd.bstStopCharging = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 8), false);
        offset += 8;
        // 8字节BSD BMS 统计数据
        cmd.bsdStatistics = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 8), false);
        offset += 8;
        // 8字节BEM 报文
        cmd.bemMsg = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 8), false);
        return cmd;
    }

    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append("#cmd304#").append("\n");
        buffer.append("v1:").append(v1).append("\n");
        buffer.append("v2:").append(v2).append("\n");
        buffer.append("充电桩编码:").append(sn).append("\n");
        buffer.append("工作状态:").append(workingStatus).append("\n");                         //
        buffer.append("车连接状态:").append(carConnStatus).append("\n");                         //
        buffer.append("BRM 车辆辨识报文:").append(carRecoMsg).append("\n");                          //
        buffer.append("VBI 报文:").append(vbiMsg).append("\n");
        buffer.append("BCP动力蓄电池充电参数:").append(bcpBattChargingParam).append("\n");
        buffer.append("BRO电池充电准备就绪状态:").append(broBattChargingReadyStatus).append("\n");
        buffer.append("BCL 电池充电需求:").append(bclBattChargingRequirement).append("\n");
        buffer.append("BCS 电池充电总状态:").append(bcsBattChargingTotalStatus).append("\n");
        buffer.append("BSM 动力蓄电池状态信 息:").append(bsmBattStatus).append("\n");
        buffer.append("BST 中止充电:").append(bstStopCharging).append("\n");
        buffer.append("BSD BMS 统计数据:").append(bsdStatistics).append("\n");
        buffer.append("BEM 报文:").append(bemMsg).append("\n");
        return buffer.toString();
    }
}
